/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package utilities;

import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import entities.MediumType;
import entities.Medium;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 *
 * @author Hacnar
 */
public class MediumConverter implements Converter{

    @Override
    public void marshal(Object o, HierarchicalStreamWriter writer, MarshallingContext mc) {
        Medium medium = (Medium) o;
        if (medium.getType().equals(MediumType.FILE)) {
            writer.startNode("file");
            writer.setValue(medium.getTitle());
            writer.endNode();
        } else {
            writer.startNode("removable");
                writer.addAttribute("title", medium.getTitle());
                writer.addAttribute("type", medium.getType().toString());
                writer.addAttribute("lended", medium.isLended() ? "true" : "false");
            writer.endNode();
        }
        writer.startNode("note");
        writer.setValue(medium.getNote());
        writer.endNode();
        writer.startNode("movies");
        if (!medium.getMovies().isEmpty()) {
            for (String s : medium.getMovies()) {
                writer.startNode("movie");
                writer.startNode("id");
                writer.setValue(s);
                writer.endNode();
                writer.endNode();
            }
        }
        writer.endNode();
    }

    @Override
    public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext uc) {
        Medium medium = new Medium();
        reader.moveDown();
        if (reader.getNodeName().equalsIgnoreCase("file")) {
            medium.setType(MediumType.FILE);
            reader.moveDown();
            medium.setTitle(reader.getValue());
            reader.moveUp();
        } else {
            Iterator it = reader.getAttributeNames();
            while(it.hasNext()) {
                if (it.next().equals("title")) {
                    medium.setTitle(reader.getAttribute("title"));
                }
                if (it.next().equals("type")) {
                    medium.setType(Enum.valueOf(MediumType.class, reader.getAttribute("type").toUpperCase()));
                }
                if (it.next().equals("lended")) {
                    medium.setLended(reader.getAttribute("lended").equals("true") ? true : false);
                }
            }
            
        }
        reader.moveUp();
        reader.moveDown();
        medium.setNote(reader.getValue());
        reader.moveUp();
        reader.moveDown();
        List<String> movies = new ArrayList<String>();
        while(reader.hasMoreChildren()) {
            reader.moveDown();
            if (reader.getNodeName().equalsIgnoreCase("movie")) {
                reader.moveDown();
                movies.add(reader.getValue());
                reader.moveUp();
            }
            reader.moveUp();
        }
        medium.setMovies(movies);
        reader.moveUp();
        return medium;
    }

    @Override
    public boolean canConvert(Class type) {
        return type.equals(Medium.class);
    }
    
    private String toUpperCamel(String string) {
        return string.substring(0,1).toUpperCase()+string.substring(1,string.length()).toLowerCase();
    }
}
